package cn.iocoder.yudao.module.hs.service.bigscreen;

import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.module.hs.controller.admin.bigscreen.vo.*;
import cn.iocoder.yudao.module.hs.dal.mysql.bigscreen.BigScreenMapper;
import cn.iocoder.yudao.module.hs.enums.shiporder.ShipOrderStatusEnum;
import cn.iocoder.yudao.module.hs.service.entryorder.EntryOrderService;
import cn.iocoder.yudao.module.hs.service.material.MaterialService;
import cn.iocoder.yudao.module.hs.service.shiporder.ShipOrderService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

@Service
@Validated
public class BigScreenServiceImpl implements BigScreenService {

    @Resource
    private ShipOrderService shipOrderService;

    @Resource
    private EntryOrderService entryOrderService;

    @Resource
    private MaterialService materialService;

    @Resource
    private BigScreenMapper bigScreenMapper;

    @Override
    public BigScreenTripartiteMaterialChartVO getTripartiteMaterialChart() {
        BigScreenTripartiteMaterialChartVO vo = new BigScreenTripartiteMaterialChartVO();
        List<WarehouseMaterialCount> shipCountList = shipOrderService.getWarehouseMaterialCountList();
        List<WarehouseMaterialCount> entryCountList = entryOrderService.getWarehouseMaterialCountList();
        List<WarehouseMaterialCount> inCountList = materialService.getWarehouseMaterialCountList();
        List<BigScreenTripartiteMaterialVO> tripartiteMaterialList = new ArrayList<>();

        // 创建一个Map来存储warehouseId到BigScreenTripartiteMaterialVO的映射
        Map<Long, BigScreenTripartiteMaterialVO> materialMap = new HashMap<>();

        // 处理出库数量列表
        for (WarehouseMaterialCount shipCount : shipCountList) {
            BigScreenTripartiteMaterialVO materialVO = materialMap.computeIfAbsent(shipCount.getWarehouseId(), k -> {
                BigScreenTripartiteMaterialVO newVO = new BigScreenTripartiteMaterialVO();
                newVO.setWarehouseId(shipCount.getWarehouseId());
                newVO.setWarehouseName(shipCount.getWarehouseName());
                return newVO;
            });
            materialVO.setShipCount(shipCount.getCount());
        }

        // 处理入库数量列表
        for (WarehouseMaterialCount entryCount : entryCountList) {
            BigScreenTripartiteMaterialVO materialVO = materialMap.computeIfAbsent(entryCount.getWarehouseId(), k -> {
                BigScreenTripartiteMaterialVO newVO = new BigScreenTripartiteMaterialVO();
                newVO.setWarehouseId(entryCount.getWarehouseId());
                newVO.setWarehouseName(entryCount.getWarehouseName());
                return newVO;
            });
            materialVO.setEntryCount(entryCount.getCount());
        }

        // 处理在库数量列表
        for (WarehouseMaterialCount inCount : inCountList) {
            BigScreenTripartiteMaterialVO materialVO = materialMap.computeIfAbsent(inCount.getWarehouseId(), k -> {
                BigScreenTripartiteMaterialVO newVO = new BigScreenTripartiteMaterialVO();
                newVO.setWarehouseId(inCount.getWarehouseId());
                newVO.setWarehouseName(inCount.getWarehouseName());
                return newVO;
            });
            materialVO.setInStorageCount(inCount.getCount());
        }

        // 将Map中的值转换为List
        tripartiteMaterialList.addAll(materialMap.values());

        // 根据entryCount、shipCount和inStorageCount的和进行降序排列
        tripartiteMaterialList.sort((o1, o2) -> {
            int sum1 = (o1.getEntryCount() == null ? 0 : o1.getEntryCount()) +
                       (o1.getShipCount() == null ? 0 : o1.getShipCount()) +
                       (o1.getInStorageCount() == null ? 0 : o1.getInStorageCount());
            int sum2 = (o2.getEntryCount() == null ? 0 : o2.getEntryCount()) +
                       (o2.getShipCount() == null ? 0 : o2.getShipCount()) +
                       (o2.getInStorageCount() == null ? 0 : o2.getInStorageCount());
            return Integer.compare(sum2, sum1); // 降序排列
        });

        vo.setTripartiteMaterialList(tripartiteMaterialList);
        return vo;
    }

    @Override
    public BigScreenMaterialCountVO getMaterialCount() {
        BigScreenMaterialCountVO vo = new BigScreenMaterialCountVO();
        vo.setShipCountToday(shipOrderService.getProductWarehouseHistoryCount(true));
        vo.setShipCountHistory(shipOrderService.getProductWarehouseHistoryCount(false));
        vo.setEntryCountToday(entryOrderService.getProductWarehouseHistoryCount(true));
        vo.setEntryCountHistory(entryOrderService.getProductWarehouseHistoryCount(false));
        vo.setInStorageCount(materialService.getProductWarehouseCount());
        vo.setShipCountMonth(shipOrderService.getProductWarehouseMonthCount());
        return vo;
    }

    @Override
    public List<BigScreenOperateRecord> operateRecord() {
        return bigScreenMapper.operateRecord();
    }

    @Override
    public List<WarehouseMaterialData> warehouseMaterialData() {
        WarehouseMaterialData warehouseMaterialInData = new WarehouseMaterialData();
        warehouseMaterialInData.setWarehouseName("深圳工厂");
        warehouseMaterialInData.setDataType(1);
        List<WarehouseMaterialCount> warehouseMaterialCounts = bigScreenMapper.warehouseMaterialInData();
        warehouseMaterialInData.setWarehouseMaterialCountList(warehouseMaterialCounts);
        List<WarehouseMaterialCount> warehouseMaterialShipCountList = bigScreenMapper.warehouseMaterialShipData();
        List<WarehouseMaterialCount> transportingCountList = new ArrayList<>();
        List<WarehouseMaterialCount> shipCountList = new ArrayList<>();
        for (int i = 0; i < warehouseMaterialShipCountList.size(); i++) {
            if (ShipOrderStatusEnum.TRANSPORTING.getStatus().equals(warehouseMaterialShipCountList.get(i).getStatus())) {
                transportingCountList.add(warehouseMaterialShipCountList.get(i));
            } else {
                shipCountList.add(warehouseMaterialShipCountList.get(i));
            }
        }
        WarehouseMaterialData warehouseMaterialTransportingData = new WarehouseMaterialData();
        warehouseMaterialTransportingData.setWarehouseName("深圳工厂");
        warehouseMaterialTransportingData.setDataType(2);
        warehouseMaterialTransportingData.setWarehouseMaterialCountList(transportingCountList);
        WarehouseMaterialData warehouseMaterialShipData = new WarehouseMaterialData();
        warehouseMaterialShipData.setWarehouseName("深圳工厂");
        warehouseMaterialShipData.setDataType(3);
        warehouseMaterialShipData.setWarehouseMaterialCountList(shipCountList);

        WarehouseMaterialData warehouseMaterialInDataJX = new WarehouseMaterialData();
        warehouseMaterialInDataJX.setWarehouseName("江西工厂");
        warehouseMaterialInDataJX.setDataType(1);
        warehouseMaterialInDataJX.setWarehouseMaterialCountList(new ArrayList<>());
        WarehouseMaterialData warehouseMaterialTransportingDataJX = new WarehouseMaterialData();
        warehouseMaterialTransportingDataJX.setWarehouseName("江西工厂");
        warehouseMaterialTransportingDataJX.setDataType(2);
        warehouseMaterialTransportingDataJX.setWarehouseMaterialCountList(new ArrayList<>());
        WarehouseMaterialData warehouseMaterialShipDataJX = new WarehouseMaterialData();
        warehouseMaterialShipDataJX.setWarehouseName("江西工厂");
        warehouseMaterialShipDataJX.setDataType(3);
        warehouseMaterialShipDataJX.setWarehouseMaterialCountList(new ArrayList<>());
        WarehouseMaterialData warehouseMaterialInDataHB = new WarehouseMaterialData();
        warehouseMaterialInDataHB.setWarehouseName("鹤壁工厂");
        warehouseMaterialInDataHB.setDataType(1);
        warehouseMaterialInDataHB.setWarehouseMaterialCountList(new ArrayList<>());
        WarehouseMaterialData warehouseMaterialTransportingDataHB = new WarehouseMaterialData();
        warehouseMaterialTransportingDataHB.setWarehouseName("鹤壁工厂");
        warehouseMaterialTransportingDataHB.setDataType(2);
        warehouseMaterialTransportingDataHB.setWarehouseMaterialCountList(new ArrayList<>());
        WarehouseMaterialData warehouseMaterialShipDataHB = new WarehouseMaterialData();
        warehouseMaterialShipDataHB.setWarehouseName("鹤壁工厂");
        warehouseMaterialShipDataHB.setDataType(3);
        warehouseMaterialShipDataHB.setWarehouseMaterialCountList(new ArrayList<>());
        WarehouseMaterialData warehouseMaterialInDataHZ = new WarehouseMaterialData();
        warehouseMaterialInDataHZ.setWarehouseName("汇总");
        warehouseMaterialInDataHZ.setDataType(1);
        warehouseMaterialInDataHZ.setWarehouseMaterialCountList(warehouseMaterialCounts);
        WarehouseMaterialData warehouseMaterialTransportingDataHZ = new WarehouseMaterialData();
        warehouseMaterialTransportingDataHZ.setWarehouseName("汇总");
        warehouseMaterialTransportingDataHZ.setDataType(2);
        warehouseMaterialTransportingDataHZ.setWarehouseMaterialCountList(transportingCountList);
        WarehouseMaterialData warehouseMaterialShipDataHZ = new WarehouseMaterialData();
        warehouseMaterialShipDataHZ.setWarehouseName("汇总");
        warehouseMaterialShipDataHZ.setDataType(3);
        warehouseMaterialShipDataHZ.setWarehouseMaterialCountList(shipCountList);

        List<WarehouseMaterialData> list = new ArrayList<>();
        list.add(warehouseMaterialInData);
        list.add(warehouseMaterialTransportingData);
        list.add(warehouseMaterialShipData);
        list.add(warehouseMaterialInDataJX);
        list.add(warehouseMaterialTransportingDataJX);
        list.add(warehouseMaterialShipDataJX);
        list.add(warehouseMaterialInDataHB);
        list.add(warehouseMaterialTransportingDataHB);
        list.add(warehouseMaterialShipDataHB);
        list.add(warehouseMaterialInDataHZ);
        list.add(warehouseMaterialTransportingDataHZ);
        list.add(warehouseMaterialShipDataHZ);
        return list;
    }

    @Override
    public List<WarehouseCustomerCountData> warehouseCustomerStatus(int queryScopeType) {
        String startDateTime = "";
        String endDataTime = "";
        if (queryScopeType == 1) {
            startDateTime = DateUtils.getTodayStartString();
            endDataTime = DateUtils.getTodayEndString();
        } else if (queryScopeType == 2) {
            startDateTime = DateUtils.getStartOfWeekString();
            endDataTime = DateUtils.getEndOfWeekString();
        } else if (queryScopeType == 3) {
            startDateTime = DateUtils.getFirstDayOfMonthString();
            endDataTime = DateUtils.getLastDayOfMonthString();
        }
        return bigScreenMapper.warehouseCustomerStatus(startDateTime, endDataTime);
    }

    @Override
    public List<WarehouseTripartiteCountData> warehouseTripartiteStatus(int queryScopeType) {
        String startDateTime = "";
        String endDataTime = "";
        if (queryScopeType == 1) {
            startDateTime = DateUtils.getTodayStartString();
            endDataTime = DateUtils.getTodayEndString();
        } else if (queryScopeType == 2) {
            startDateTime = DateUtils.getStartOfWeekString();
            endDataTime = DateUtils.getEndOfWeekString();
        } else if (queryScopeType == 3) {
            startDateTime = DateUtils.getFirstDayOfMonthString();
            endDataTime = DateUtils.getLastDayOfMonthString();
        }
        List<WarehouseTripartiteCountData> warehouseTripartiteCountData = bigScreenMapper.warehouseTripartiteStatus(startDateTime, endDataTime);
        for (int i = 0; i < warehouseTripartiteCountData.size(); i++) {
            WarehouseTripartiteCountData data = warehouseTripartiteCountData.get(i);
            List<CustomerCountData> customerCountData = bigScreenMapper.customerCountData(data.getWarehouseId(), startDateTime, endDataTime);
            data.setCustomerCountDataList(customerCountData);
        }
        return warehouseTripartiteCountData;
    }

}
